package com.xjwlmq.exam20241012;

import com.xjwlmq.dx.util.BaseDao;
import com.xjwlmq.exam20241011.DictType;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @ClassName WorkerDaoImpl
 * @Description TODO:
 * @Author 12994
 * @Date 2024/10/12 20:41
 * @Version 1.0
 **/
public class WorkerDaoImpl extends BaseDao implements WorkerBaseDao {
    @Override
    public ArrayList<Worker> getWorkers() {
        ArrayList<Worker> workers = new ArrayList<>();
        Worker thisWorker = null;
        try {
            this.rs = baseQuery("SELECT * FROM t_worker;");
            if (this.rs == null) {
                throw new NullPointerException("查询结果为空!");
            }
            while (this.rs.next()) {
                thisWorker = new Worker();
                thisWorker.setWorkerId(this.rs.getInt("worker_id"));
                thisWorker.setDepartmentName(this.rs.getString("department_name"));
                thisWorker.setWorkerDate(this.rs.getString("worker_date"));
                thisWorker.setWages(this.rs.getDouble("wages"));
                thisWorker.setPolitics(this.rs.getString("politics"));
                thisWorker.setName(this.rs.getString("name"));
                thisWorker.setBirthDate(this.rs.getString("birth_date"));
                workers.add(thisWorker);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return workers;
    }

    @Override
    public int addWorker(Worker worker) {
        return baseUpdate("INSERT INTO t_worker " +
                        "(worker_id, department_name, " +
                        "worker_date, wages, politics, " +
                        "name, birth_date) " +
                        "VALUES (null,?,?,?,?,?,?);",
                worker.getDepartmentName(),
                worker.getWorkerDate(),
                worker.getWages(),
                worker.getPolitics(),
                worker.getName(),
                worker.getBirthDate());
    }

    @Override
    public int updateWorkerViaDictId(Worker worker) {
        return baseUpdate("UPDATE t_worker SET " +
                        "department_name=?, " +
                        "worker_date=?, wages=?, politics=?, " +
                        "name=?, birth_date=? " +
                        "where worker_id=?;",
                worker.getDepartmentName(),
                worker.getWorkerDate(),
                worker.getWages(),
                worker.getPolitics(),
                worker.getName(),
                worker.getBirthDate(),
                worker.getWorkerId());
    }

    @Override
    public int deleteWorkerViaWorkerId(String workerId) {
        return baseUpdate("DELETE FROM t_worker WHERE worker_id=?;", workerId);
    }

    public ArrayList<Worker> getWorkers(int workerId) {
        ArrayList<Worker> workers = new ArrayList<>();
        Worker thisWorker = null;
        try {
            this.rs = baseQuery("SELECT * FROM t_worker WHERE worker_id=?;", workerId);
            if (this.rs == null) {
                throw new NullPointerException("查询结果为空!");
            }
            while (this.rs.next()) {
                thisWorker = new Worker();
                thisWorker.setWorkerId(this.rs.getInt("worker_id"));
                thisWorker.setDepartmentName(this.rs.getString("department_name"));
                thisWorker.setWorkerDate(this.rs.getString("worker_date"));
                thisWorker.setWages(this.rs.getDouble("wages"));
                thisWorker.setPolitics(this.rs.getString("politics"));
                thisWorker.setName(this.rs.getString("name"));
                thisWorker.setBirthDate(this.rs.getString("birth_date"));
                workers.add(thisWorker);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return workers;
    }
}
